﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using S3.DataModel;
using S3.Biz;
using S3.Common;

namespace S3
{
    public partial class FinishSession : S3FormBase
    {
        public FinishSession()
        {
            InitializeComponent();
            gridSessionGame.AutoGenerateColumns = false;
            gridSessionServices.AutoGenerateColumns = false;
            lblStaffName.Text = Constants.StaffName;
            LoadDailyData();
        }

        private void LoadDailyData()
        {
            #region Daily

            decimal totalDailyGameMoney = 0;
            using (var manager = new DailyTimingEntryManager())
            {
                List<ExtendDailyEntry> dailyGameMoneyList = manager.GetFinishedBySession(ref totalDailyGameMoney);
                gridSessionGame.DataSource = dailyGameMoneyList;

                lblGameMoney.Text = Utils.StringFormatMoney(totalDailyGameMoney);
            }

            decimal totalServiceMoney = 0;
            using (var manager = new DailyItemEntryManager())
            {
                List<ExtendService> dailyServiceMoneyList = manager.GetPaidBySession(ref totalServiceMoney);
                gridSessionServices.DataSource = dailyServiceMoneyList;

                lblServiceMoney.Text = Utils.StringFormatMoney(totalServiceMoney);
            }

            lblTotalDailyMoney.Text = Utils.StringFormatMoney(totalDailyGameMoney + totalServiceMoney);

            #endregion
        }

        /// <summary>
        /// Close form if click ESC
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>

        private void btnFinishSession_Click(object sender, EventArgs e)
        {
            if (XtraMessageBox.Show("Bạn chắc chắn muốn bàn giao ca này?", "Chú ý", MessageBoxButtons.YesNo) ==
                DialogResult.Yes)
            {
                using (var manager = new DailyTimingEntryManager())
                {
                    manager.FinishSession(Constants.StaffName);
                }

                using (var manager = new WorkSessionManager())
                {
                    manager.DoHandOver(lblStaffName.Text);
                }

                Close();
            }
        }

        #region ProcessCmdKey

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            try
            {
                if (msg.WParam.ToInt32() == (int) Keys.Escape)
                {
                    Close();
                }
                else
                {
                    return base.ProcessCmdKey(ref msg, keyData);
                }
            }
            catch (Exception Ex)
            {
                XtraMessageBox.Show("Key Overrided Events Error:" + Ex.Message);
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }

        #endregion
    }
}